package com.onejson.ojmall.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import com.onejson.ojmall.common.domain.PageResult;
import com.onejson.ojmall.common.domain.ResponseDTO;
import com.onejson.ojmall.common.util.OjPageUtil;
import com.onejson.ojmall.dao.SRoleDataScopeDao;
import com.onejson.ojmall.model.entity.SRoleDataScopeEntity;
import com.onejson.ojmall.model.form.SRoleDataScopeBatchForm;
import com.onejson.ojmall.model.form.SRoleDataScopeForm;
import com.onejson.ojmall.model.form.SRoleDataScopeQueryForm;
import com.onejson.ojmall.model.vo.SRoleDataScopeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色的数据范围 Service 层
 *
 * @author wxx
 */
@Service
@Slf4j
public class SRoleDataScopeService extends ServiceImpl<SRoleDataScopeDao, SRoleDataScopeEntity> {

    @Resource
    private SRoleDataScopeDao sRoleDataScopeDao;

    /**
     * 获取角色的数据范围列表
     */
    public PageResult<SRoleDataScopeVO> getList(SRoleDataScopeQueryForm queryForm) {
        Page<?> page = OjPageUtil.convert2PageQuery(queryForm);
        List<SRoleDataScopeVO> list = sRoleDataScopeDao.query(page, queryForm);
        return OjPageUtil.convert2PageResult(page, list);
    }

    /**
     * 查询角色的数据范围
     */
    public SRoleDataScopeVO queryById(String detailId) {

        SRoleDataScopeEntity detailEntity = sRoleDataScopeDao.selectById(detailId);
        SRoleDataScopeVO sRoleDataScopeVO = new SRoleDataScopeVO();

        try {
            BeanUtils.copyProperties(sRoleDataScopeVO, detailEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return sRoleDataScopeVO;

    }


    /**
     * 【角色的数据范围】 - 添加
     */
    public ResponseDTO<String> addSRoleDataScope(SRoleDataScopeForm addForm) {

        SRoleDataScopeEntity entity = new SRoleDataScopeEntity();

        addForm.setId(null);

        try {
            BeanUtils.copyProperties(entity, addForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        sRoleDataScopeDao.insert(entity);
        return ResponseDTO.ok();
    }

    /**
     * 【角色的数据范围】 - 更新
     */
    public ResponseDTO<String> updateSRoleDataScope(SRoleDataScopeForm updateForm) {

        SRoleDataScopeEntity entity = new SRoleDataScopeEntity();

        try {
            BeanUtils.copyProperties(entity, updateForm);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        sRoleDataScopeDao.updateById(entity);
        return ResponseDTO.ok();
    }


    /**
     * 【角色的数据范围】- 批量更新
     */
    public ResponseDTO<String> batchUpdateSRoleDataScope(SRoleDataScopeBatchForm sRoleDataScopeBatchForm) {

        SRoleDataScopeEntity entity = new SRoleDataScopeEntity();
        List<SRoleDataScopeEntity> sRoleDataScopeEntityList = new ArrayList<>();

        try {
            for (SRoleDataScopeForm sRoleDataScopeForm : sRoleDataScopeBatchForm.getSRoleDataScopeFormList()) {

                sRoleDataScopeForm.setCreateTime(null);
                sRoleDataScopeForm.setUpdateTime(null);
//                entity.setRemark(sRoleDataScopeBatchForm.getRemark());
                BeanUtils.copyProperties(entity, sRoleDataScopeForm);
                sRoleDataScopeEntityList.add(entity);
            }
        } catch (Exception e) {
            System.out.println("错误消息");
            System.out.println(e.getMessage());
        }
        this.updateBatchById(sRoleDataScopeEntityList);
        return ResponseDTO.ok();
    }


    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        sRoleDataScopeDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String id) {
        if (null == id) {
            return ResponseDTO.ok();
        }

        sRoleDataScopeDao.deleteById(id);
        return ResponseDTO.ok();
    }


}

